'use strict';

var _ = require('underscore');
var Promise = require('bluebird');

module.exports = function(sequelize, DataTypes) {
    //栏目表
    var Category = sequelize.define('Category', {
        code: {
            type: DataTypes.STRING,
            comment: '序号'
        },
        name: {
            type: DataTypes.STRING,
            comment: '名称',
            allowNull: false,
            unique: true,
            validate: {
                notEmpty: {
                    msg: '名称不允许为空'
                },
                len: {
                    args: [1, 200],
                    msg: '长度不能大于200字符'
                }
            }
        }
    }, {
        charset: 'utf8',
        classMethods: {
            associate: function(models) {
                Category.hasMany(models.Article, {
                    constraints: false
                });
            },
            fetch: function(op) {
                return this.findAll(_.extend({
                    order: ['code'],
                    raw: true
                }, op || {}));
            }
        }
    });

    return Category;
};


